package org.groovymud.shell.security.callbacks;

import org.groovymud.shell.io.TerminalIO;

import javax.security.auth.callback.NameCallback;
import java.io.IOException;
import java.util.Map;

public class SelfHandlingNameCallback extends NameCallback implements SelfHandlingCallback{
    public SelfHandlingNameCallback(String prompt) {
        super(prompt);
    }

    public void handle(TerminalIO terminalIO, Map<String, Object> callbackData) throws IOException {
                String username = requestUsername(terminalIO);
                setName(username);
    }
    protected String requestUsername(TerminalIO terminalIO) throws IOException {
           terminalIO.write(getPrompt());
           terminalIO.flush();
           String username = terminalIO.readln(false);

           return username;
       }

}
